package com.lagou;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zy
 * @version 1.0
 * @date 2021/2/28 13:27
 */
public class Monitor {

    private int numberOf = 0;
    private List<Integer> time = new ArrayList<>();
    private String methodName;

    static Monitor getInstance(){
        Monitor monitor = new Monitor();
        return monitor;
    }

    public synchronized void add(int ms,String methodName){
        numberOf++;
        time.add(ms);
        this.methodName = methodName;
    }

    public String getMethodName(){
        return methodName;
    }

    public int getTp(float f){
        int round = Math.round(numberOf * f);
        time = time.stream().sorted((i1, i2) -> i1 - i2).collect(Collectors.toList());
        return time.get(round-1);
    }

    public int getNumberOfRequests(){
        return numberOf;
    }

}
